/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using ResourceManager = FourRoads.Links.Controls.ResourceManager;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for ResLinkSectionView.
	/// </summary>
	public class ResLinkChildSectionView : ResLinkBaseSectionView
	{
		#region Child Controls
			RepeaterPlusNone _Sections = null;
			Literal _SectionTitle = null;
			Literal		 _SecDescription = null;
		/*	SectionTrackedLinkButton	_Notification = null;
			HtmlGenericControl	_SuggestSection = null;
			ModalLink	_SugestLinkButton = null;
			RssButton _RssButton = null;*/
		#endregion

		private ScrollPosMgr _ScrollMgr = new ScrollPosMgr();

		public ResLinkChildSectionView()
		{

		}

		protected override void AttachChildControls()
		{
			_Sections = FindControl("SectionDataList") as RepeaterPlusNone;
			_SecDescription =  FindControl("SecDescription") as Literal;
			_SectionTitle  = FindControl("SectionTitle") as Literal;
			_Sections.ItemDataBound  += new RepeaterItemEventHandler(_Sections_ItemDataBound);
		/*	_Notification = FindControl("SectionNotification") as SectionTrackedLinkButton;	
			_SuggestSection = FindControl("SuggestLinkBox") as HtmlGenericControl;
			_SugestLinkButton = FindControl("SuggestLinkBtn") as ModalLink;
			_RssButton = FindControl("RssButton") as RssButton; */
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
		}

		protected override void OnInit(EventArgs e)
		{
			this.EnableViewState = true;
			Page.Load += new EventHandler(_ScrollMgr.Page_LoadScrollPosTrack);
			base.OnInit (e);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if (ThisSection.IsActive == false)
			{
				Context.Response.Redirect(Globals.GetSiteUrls().Message((CSExceptionType) RlMessageTypes.ResourceLinkSectionClosed));
			}

			if (!Page.IsPostBack)
			{
				DataBind();
			}

		//	CreateSugestLinkScript();

		//	_SugestLinkButton.Url = ResLinkUrls.Instance().SuggestLink(ThisSection.SectionID);
		}
/*
		private void BindNotification()
		{	
			_Notification.SectionID = ThisSection.SectionID;
			
			_Notification.IsTracked = ThisSection.GetSectionSubscriptionType() == EmailSubscriptionType.None ? false : true;
		}
*/
		public override void DataBind()
		{
			base.DataBind();
	
			_Sections.DataSource = ThisSection.AdjustedSections;
			_SectionTitle.Text = ThisSection.Name;

			if (_SecDescription != null)
			{
				_SecDescription.Text = ThisSection.Description.Replace("\r\n" , "<br />");

				//Hide the description if it is empty
				if (_SecDescription.Text == string.Empty || ThisSection.SectionID < 1)
				{
					System.Web.UI.Control ctrl = FindControl("descriptionBox");
					if (ctrl != null)
						ctrl.Visible = false;
				}

				_SecDescription.Visible = true;
			}
			
			_Sections.DataBind();
/*
			if (ThisSection.SectionID < 1)
			{
				System.Web.UI.Control ctrl = FindControl("trackingBox");
				if (ctrl != null)
					ctrl.Visible = false;
			}
			else
			{
				_RssButton.NavigateUrl = ResLinkUrls.Instance().RssSection(ThisSection.SectionID);
				BindNotification();
			}

			if (ThisSection.EnableSuggestLink && this.AddLinksRight && ThisSection.SectionID > 0)
			{
				_SuggestSection.Visible = true;
			}
			else
			{
				_SuggestSection.Visible = false;
			}
			*/
		}
/*
		public void CreateSugestLinkScript()
		{
			StringBuilder str = new StringBuilder();

			str.Append("<script language='javascript' type='text/javascript' >function linkSuggested(id){");
			//need to navigate back to this page but ad a postid so that it can be linked to using a bookmark
			//if its -1 then a status message will be shown
			str.AppendFormat("if (id != '0') window.location = '{0}&PostID=' + id;" , ResLinkUrls.Instance().ViewSection(ThisSection.SectionID));

			str.Append("}");
			str.Append("</script>");

			Page.RegisterClientScriptBlock("SuggestLinkCallBackFunc", str.ToString());
		}
*/
		private void _Sections_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || 
				e.Item.ItemType == ListItemType.AlternatingItem )
			{
				HyperLink hyp = e.Item.FindControl("SectionNameLnk") as HyperLink;

				ResLinkSection rls = ((ResLinkSection)e.Item.DataItem);		

				if (_Config.DisplaySectionCounts || _Config.DisplayLinkCounts)
				{
					hyp.Text = FormatSectionLinkCounts(rls);
				}
				else
				{
					hyp.Text = rls.Name;
				}

				if (rls.IsActive)
				{
					hyp.NavigateUrl = ResLinkUrls.Instance().ViewSection( System.Convert.ToInt32(rls.SectionID) );
					hyp.ToolTip = string.Format(ResourceManager.GetString("ShowResourceLinks_ToolTip") ,  rls.Name);// "Show resources and links for: " + ((ResLinkSection)e.Item.DataItem).Name;
				}
				else
				{
					hyp.ToolTip = ResourceManager.GetString("ShowResourceLinks_SectionClosed");
				}
			}
		}
	}
}
